﻿using System;
using UnityEngine;

public class InfoGameOfLife : MonoBehaviour {
	// === Unity ======================================================================================================
	public UILabel Label;
	public UISprite SpriteLife1;
	public UISprite SpriteLife2;
	public UISprite SpriteLife3;

	private void Awake() {
		if (Label == null) {
			throw new ArgumentException("Label is null");
		}
		if (SpriteLife1 == null) {
			throw new ArgumentException("SpriteLife1 is null");
		}
		if (SpriteLife2 == null) {
			throw new ArgumentException("SpriteLife2 is null");
		}
		if (SpriteLife3 == null) {
			throw new ArgumentException("SpriteLife3 is null");
		}

		Label.text = "life:";
	}

	private void Update() {
		RefreshSprite();
	}

	// === Private ====================================================================================================
	private const string SPRITE_HEART_RED = "Heart_red";
	private const string SPRITE_HEART_GRAY = "Heart_gray";
	private UISpriteData _spriteHeartGray;
	private int _life;

	private void RefreshSprite() {
		var lifeNew = PlayerStats.GetInstance.Life;
		if (_life == lifeNew) {
			return;
		}
		_life = lifeNew;

		SpriteLife1.spriteName = 1 <= _life ? SPRITE_HEART_RED : SPRITE_HEART_GRAY;
		SpriteLife2.spriteName = 2 <= _life ? SPRITE_HEART_RED : SPRITE_HEART_GRAY;
		SpriteLife3.spriteName = 3 <= _life ? SPRITE_HEART_RED : SPRITE_HEART_GRAY;
	}
}
